# # 打印30以内能被三整除的数
# print(list(filter(lambda item: item % 3 == 0, range(1, 31))))
#
# # ------------------------------------------------------------------------------
# # ##列表推导式
# # or i in range(1, 31) --> if i % 3 == 0 --> return i --> 再将所有的结果都放到列表中
# print([i for i in range(1, 31) if i % 3 == 0])
#
# # 打印30以内能被三整除的数的平方
# print([i**2 for i in range(1, 31) if i % 3 == 0])
#
# # 嵌套推导式
# names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe']]
# print([name for lst in names for name in lst if name.count('e') >= 2])
#
# # 打印100以内能被3整除的数， 如果是偶数返回0，奇数返回这个数
# # for循环后面是确定范围，前面是条件返回
# print([i if i % 2 else 0 for i in range(100) if not i % 3])
# print([i if i % 2 != 0 else 0 for i in range(100) if i % 3 == 0])
#
# # ------------------------------------------------------------------------------
# # ##集合推导式
# str1 = "lajdlfjoi2olkljwejldlllwes"
# print(set(str1))
# print({i for i in str1})
#
# # ------------------------------------------------------------------------------
# # ##字典推导式
# str1 = "adfasdfddsd"
# dic = {'a': 1, 'b': 2}
# print({i: str1.count(i) for i in str1})
# print({value: key for key, value in dic.items()})
#
# # ------------------------------------------------------------------------------
# # 字符数小于3，大写输出
# q1 = ['a', 'ab', 'abc', 'abcd', 'abcde']
# print([i.upper() for i in q1 if len(i) < 3])
#
# # 求元组（x，y）x是0-5之间的偶数，y是0-5之间的奇数组成
# q1 = [(0, 1), (1, 0), (2, 1), (1, 2), (6, 7)]
# print([(x, y) for x, y in q1 if not x % 2 and y % 2 and x < 6 and y < 6])
# print([(x, y) for x in range(6) if not x % 2 for y in range(6) if y % 2])
#
# # 快速更换key和value
# q3 = {'a': 10, 'b': 34}
# print({value: key for key, value in q3.items()})
#
# # 合并大小写对应的value值，将k统一成小写
# q4 = {'B': 3, 'a': 1, 'b': 6, 'c': 3, 'A': 4}
# print({i.lower(): q4.get(i.lower(), 0) + q4.get(i.upper(), 0) for i in q4})

# 后面的for循环是筛选出数据只能用if不能接else，前面的可以加if else来返回处理数据，或不使用if else

# ---------------------------------------------------------------------------------
# 迭代器 --》iterator
# 可迭代对象
# 如果这个变量用dir（）可以看到__iter__就说明这个容器时可迭代对象类型
# 使用Iterable判断 --》 from collections import Iterable
# isinstance(判断对象, Iterable) --》 返回True False
# 可迭代对象用__iter__返回一个可迭代对象
# 可迭器用__next__返回下一个元素
# 如果容器中没有更多元素了，就会抛出异常

# 可迭代对象是通过__iter__返回迭代器的对象为可迭代对象
# 迭代器是含有__iter__和__next__这两个方法，__iter__返回自身，__next__返回下一个值

# a = [1, 2, 3]
# # 可迭代对象通过这两种方法返回迭代器
# print(a.__iter__())
# print(iter(a))
# <list_iterator object at 0x000000001B4274C0>
# <list_iterator object at 0x000000001B4274C0>
# print(next(a.__iter__()))
# print(next(iter(a)))
# 1
# 1

# 生成无限序列
# 迭代器模块
# from itertools import count
# 从10开始
# counter = count(start=10)
# print(counter, type(counter))
# while True:
#     print(next(counter))

# 从一个有限的序列中生成无限序列
# from itertools import cycle
# import time
# weeks = cycle(["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"])
# print(type(weeks), weeks)
# for i in weeks:
#     print(i)
#     time.sleep(1)

# # 从无限的序列中生成有限序列
# from itertools import count, islice
# counter = count(start=1)
# print(counter, type(counter))
# # 左开右闭
# s = islice(counter, 0, 10)
# for i in s:
#     print(i)

# --------------------------------------------------------------------------------------------
# ##生成器 ——》 generator
# # 生成器表达式
# a = [1, 2, 3]
# print(i for i in a)
#
# # 生成器函数 --> yield 关键字
#
#
# def get_content():
#     print("start yield....")
#     yield 3
#     print("second yield....")
#     yield 4
#     print("third yield....")
#     yield 5
#     print("end....")
#
#
# a = get_content()
# # 当调用next值时，遇到yield就会暂停运行，并且返回yield的值，直到运行下一个yield
# print(dir(a))
# print(next(a))
# print(next(a))
# print(next(a))


# def aaa():
#     i = 0
#     while True:
#         i += 1
#         yield i
#
#
# a = aaa()
# print(next(a))
# print(next(a))
# print(next(a))
# print(next(a))

# from itertools import islice
#
#
# def Fib():
#     i = 0
#     j = 1
#     while True:
#         yield i
#         i, j = j, i + j
#
#
# a = Fib()
# print(list(islice(a, 0, 10)))


# -----------------------------------------------------------------------------------
# 自己生成的迭代器
# def counter(start_at=0):
#     count = start_at
#     while True:
#         val = (yield count)
#         print(f"val1 is {val}")
#         if val is not None:
#             print(f"val2 is {val}")
#             count = val
#         else:
#             count += 1
#
# # yield 后的数被返回了后，就会使该数为None，需要重新赋值
# count = counter()
# print(next(count))
# print()
# print(next(count))
# print()
# count.send(10)
# print(next(count))

# def f1():
#     # yield一个可迭代对象返回的还是可迭代对象
#     yield range(10)
#
#
# def f2():
#     # yield一个迭代器返回的是下一个值
#     yield from range(10)
#
#
# it1 = f1()
# it2 = f2()
# print(it1)
# print(it2)
# for i in it1:
#     print(i)
# for i in it2:
#     print(i)


# import random
# with open("big.txt", 'w+') as fp:
#     for i in range(10**7):
#         fp.write("a"*random.randint(1, 100)+'\n')
# def f():
#     with open("big.txt") as fp:
#         SIZE = 10000000
#         while True:
#             a = len(fp.read(SIZE))
#             if a:
#                 yield a
#             else:
#                 # 如果最后没有得到数据后就退出了
#                 return
#
#
# a = f()
# for i in a:
#     print(i)

# a = f()
# print(next(a))
# print(next(a))